<?php
namespace Macaca\Module\Admin;

use Macaca;
use Monkey;

class GoodsCategory {

    /**
     * @var Monkey\App
     */
    public $app;

    public $modelGC; //'gc_id', 'parent_id', 'gc_name', 'caption', 'sort', 'keywords', 'description'
    public $modelPriKey;
    private $modelCacheKey = 'MacacaGC';

    /**
     * @param Monkey\App $app
     */
    public function __construct($app) {
        $this->app = $app;
        $this->modelGC = new Macaca\Model\Admin\GoodsCategory($app);
        $this->modelPriKey = $this->modelGC->priKey;
    }

    /**
     * @return array //'gc_id', 'parent_id', 'gc_name', 'caption', 'sort', 'keywords', 'description'
     */
    public function getGoodsCategoryList() {
        $result = array();
        $cache = $this->app->cache();
        if (!$cache->fetch($this->modelCacheKey, $result)) {
            $result = $this->modelGC->findAll();
            $cache->store($this->modelCacheKey, $result);
        }
        return $result;
    }

    public function getSubCategory($parentId = 0) {
        $gc = $this->getGoodsCategoryList();
        $tree = array();
        $this->buildTree($gc, $tree, $parentId);

        return $tree;
    }

    public function getParentList($gcId, $parentId) {
        $list = $this->getSubCategory(0);
        $doc = $this->app->view()->document();

        $parentList[] = $doc->selectOption('创建新的顶级商品种类', 0, $parentId == 0);

        $parent = null;
        $pk = $this->modelPriKey;
        foreach ($list as $item) {
            if ($item[$pk] == $gcId) {
                $parent = $item;
                continue;
            }

            if (!is_null($parent)) {
                if (strlen($parent['fix']) < strlen($item['fix'])) {
                    continue;
                }
                else {
                    $parent = null;
                }
            }

            $parentList[] = $doc->selectOption($item['fix'] . $item['caption'], $item[$pk], $parentId == $item[$pk]);
        }

        return implode("\n", $parentList);
    }

    public function getCategoryList($acId = 0) {
        $list = $this->getSubCategory(0);
        $doc = $this->app->view()->document();

        $categoryList = null;
        $pk = $this->modelPriKey;

        foreach ($list as $item) {
            $categoryList[] = $doc->selectOption($item['fix'] . $item['caption'], $item[$pk], $acId == $item[$pk]);
        }

        if (is_array($categoryList)) {
            return implode("\n", $categoryList);
        }
        else {
            return '';
        }
    }

    /**
     * 获取一个空商品种类 //'gc_id', 'parent_id', 'gc_name', 'caption', 'sort', 'keywords', 'description'
     * @return mixed
     */
    public function getEmptyItem() {
        $data['gc_id'] = '';
        $data['parent_id'] = '0';
        $data['gc_name'] = 'new_column';
        $data['caption'] = '新商品种类';
        $data['sort'] = '50';
        $data['keywords'] = 'Macaca 商品种类';
        $data['description'] = '新的商品种类';
        return $data;
    }

    public function getItemById($acId) {
        $data = $this->modelGC->find($this->modelPriKey, $acId);

        if ($data) {
            return $data;
        }
        else {
            return array();
        }
    }

    public function deleteItemById($acId) {
        if ($this->modelGC->delete($this->modelPriKey, $acId)) {
            $this->resetCache();
            return true;
        }
        else {
            return false;
        }
    }

    public function saveItem($data = array()) {
        $pk = $this->modelPriKey;
        if (!empty($data[$pk]) and !is_numeric($data[$pk])) {
            return false;
        }

        $dataTemp = $this->getEmptyItem();
        unset($dataTemp[$pk]);
        foreach ($dataTemp as $k => $v) {
            if (!isset($data[$k])) {
                return false;
            }
            $dataTemp[$k] = $data[$k];
        }

        if (!empty($data[$pk])) {
            $result = $this->modelGC->updateByPriKey($data[$pk], $dataTemp);
        }
        else {
            $result = $this->modelGC->insert($dataTemp);
        }

        if ($result) {
            $this->resetCache();
        }

        return $result;
    }


    protected function resetCache() {
        $this->app->cache()->delete($this->modelCacheKey);
    }

    protected function buildTree(&$source, &$tree, $parentId = 0, $deep = 0, $fix = '- ') {
        $isBreak = false;
        $pk = $this->modelPriKey;
        foreach ($source as $k => $item) {

            if ($item['parent_id'] != $parentId) {
                continue;
            }

            $item['fix'] = str_repeat($fix, $deep);
            $tree[] = $item;
            unset($source[$k]);
            $this->buildTree($source, $tree, $item[$pk], $deep + 1);
            $isBreak = true;
            break;
        }

        if (!empty($source) and $isBreak) {
            $this->buildTree($source, $tree, $parentId, $deep);
        }
    }
} 